<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    const infos = {
      friends: ["kobe", "james", "curry"],
      [Symbol.iterator]: function () {
        let index = 0
        const infosIterator = {
          // 使用匿名函数,可以拿到上层的this
          next: () => {
            if (index < this.friends.length) {
              return { done: false, value: this.friends[index++] }
            } else {
              return { done: true }
            }
          }
        }
        return infosIterator
      }
    }
    for (const info of infos) {
      console.log(info);
    }

    console.log("====================================");

    const infos1 = {
      name: 'why',
      age: 18,
      height: 1.88,
      [Symbol.iterator]: function () {
        const keys = Object.keys(this)
        let index = 0
        const iterator = {
          next: () => {
            if (index < keys.length) {
              return { done: false, value: keys[index++] }
            } else {
              return { done: true }
            }
          }
        }
        return iterator
      }
    }
    for (const info1 of infos1) {
      console.log(info1);
    }


  </script>
</body>

</html>